package tech.aistar.corejava.day12;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/7/30
 * @Version: 1.0
 */
public class TreeSetSortDemo {
    public static void main(String[] args) {
        Book b1 = new Book(1, "了不起的盖茨比", "忘了", 20.0);
        Book b2 = new Book(2, "刀锋", "毛姆", 35.0);
        Book b3 = new Book(3, "鬼吹灯", "天下霸唱", 296.0);
        Book b4 = new Book(4, "盗墓笔记", "南派三叔", 329.0);

        TreeSet<Book> books = new TreeSet<>(new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                //需要对价格降序
                if(o1.getPrice() < o2.getPrice()){
                    return 1;
                }else if(o1.getPrice() > o2.getPrice()) {
                    return -1;
                }else{
                    return 0;
                }
            }
        });
        books.add(b1);
        books.add(b2);
        books.add(b3);
        books.add(b4);

        books.forEach(System.out::println);
    }
}
